﻿using Acme.BookStore.BasicInformations;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.ReserveInformations
{
    /// <summary>
    /// 预留信息接口实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ReserveInformationServices : ApplicationService, IReserveInformationServices
    {
        /// <summary>
        /// 注入预留信息表
        /// </summary>
        private readonly IRepository<ReserveInformation> repository;

        /// <summary>
        ///构造函数
        /// </summary>
        public ReserveInformationServices(IRepository<ReserveInformation> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        ///删除预留信息
        /// </summary>
        /// <param name="ReserveInformationId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("DeleteReserveInformation")]
        public async Task<ApiResult> DeleteReserveInformation(Guid ReserveInformationId)
        {
            await repository.DeleteAsync(x => x.Id == ReserveInformationId);
            return ApiResult.Ok();
        }

        /// <summary>
        ///显示预留信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("GetReserveInformation")]
        public async Task<ApiResult> GetReserveInformation(ReserveInformationDTO dto)
        {
            var list = await repository.GetListAsync();
            return ApiResult.Ok(list);
        }

        /// <summary>
        /// 添加预留信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("PostReserveInformation")]
        public async Task<ApiResult> PostReserveInformation(ReserveInformationDTO dto)
        {
            var list = ObjectMapper.Map<ReserveInformationDTO,ReserveInformation>(dto);
            await repository.InsertAsync(list);
            return ApiResult.Ok(data: list);
        }

        /// <summary>
        /// 修改预留信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("PutReserveInformation")]
        public async Task<ApiResult> PutReserveInformation(ReserveInformationUpdateDTO dto)
        {
            var list = await repository.FirstOrDefaultAsync(x => x.Id == dto.ReserveInformationId);
            if (list == null)
            {
                return ApiResult.Fail(msg: "预留信息不能为空");
            }

            ObjectMapper.Map(dto, list);
            await repository.UpdateAsync(list);
            return ApiResult.Ok(data: list);
        }
    }
}
